<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="(item, index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{ active: index === activeIndex }"
        @click="changeRole(index)"
      >
        <div class="role-info">
          <svg-icon
            :icon-class="index === activeIndex ? 'user-active' : 'user'"
            class="icon"
          />
          {{ item.roleName }}
        </div>
        <!--   
        <el-dropdown>下拉菜单
          trigger	触发下拉的行为
         -->
        <div class="more">
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>

            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                @click.native="$router.push(`/roleEdit?id=${item.roleId}`)"
              >
                编辑角色
              </el-dropdown-item>

              <el-dropdown-item @click.native="delRole(item.roleId)"
                >删除</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>

      <el-button class="addBtn" size="mini" @click="$router.push('/roleAdd')"
        >添加角色</el-button
      >
    </div>
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
            <!--  组件中渲染模版 -->
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title">{{ item.title }}</div>
              <!-- 
        <el-tree/>的用法
          show-checkbox	节点是否可被选择
          default-expand-all	是否默认展开所有节点
          node-key	每个树节点用来作为唯一标识的属性，整棵树应该是唯一的
          props	配置选项，具体看下表
            label	指定节点标签为节点对象的某个属性值	
            children	指定子树为节点对象的某个属性值	
            disabled	指定节点选择框是否禁用为节点对象的某个属性值	
            isLeaf	指定节点是否为叶子节点，仅在指定了 lazy 属性的情况下生效
           -->
              <el-tree
                ref="tree"
                :props="{ label: 'title' }"
                :data="item.children"
                show-checkbox
                default-expand-all
                node-key="id"
              />
            </div>
          </div>
        </el-tab-pane>

        <!-- 成员 -->
        <el-tab-pane :label="`成员(${roleUserList.length})`" name="user">
          <div class="user-wrapper">
            <el-table :data="roleUserList" style="width: 100%">
              <el-table-column type="index" width="250" label="序号" />
              <el-table-column prop="name" label="员工姓名" />
              <el-table-column prop="userName" label="登录账号" />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import {
  getRoleListAPI,
  getTreeListAPI,
  getRoleDetailAPI,
  getRoleUserAPI,
  delRoleUserAPI,
} from "@/api/System";
export default {
  name: "Role",
  data() {
    return {
      // 成员列表数据
      roleUserList: [],
      // 默认打开tree
      activeName: "tree",
      // 当前选中角色的索引
      activeIndex: 0,
      // 角色列表
      roleList: [],
      // 权限树形列表
      treeList: [],
      // 当前角色权限点列表
      perms: [],
    };
  },

  // 生命周期里先以第一项roleId获取
  async mounted() {
    // 先获取角色列表和可选权限列表
    await this.getRoleList();
    await this.getTreeList();
    // 再获取当前角色下的权限列表
    this.getRoleDetail(this.roleList[0].roleId);
    this.getRoleUserList(this.roleList[0].roleId);
  },
  methods: {
    delRole(roleId) {
      this.$confirm("是否确认删除当前角色?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        await delRoleUserAPI(roleId);
        this.getRoleList();
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      });
    },
    // 封装获取当前角色下的成员的方法
    async getRoleUserList(roleId) {
      const res = await getRoleUserAPI(roleId);
      this.roleUserList = res.data.rows;
    },
    // 获取当前角色详情
    async getRoleDetail(roleId) {
      const res = await getRoleDetailAPI(roleId);
      // console.log(res.data);

      // 3. 将获取到的权限数据赋值给treeList
      const pers = res.data.perms; // 获取角色对应的权限点数据[[2000,2005],[],[]]
      const trees = this.$refs.tree; // 获取el-tree组件对象
      // 根据权限点数组一一设置给el-tree
      pers.forEach((items, index) => {
        // console.log(items, index);
        trees[index].setCheckedKeys(items);
      });
    },
    // 递归处理函数
    addDisabled(treeList) {
      treeList.forEach((item) => {
        item.disabled = true;
        // 递归处理
        if (item.children) {
          this.addDisabled(item.children);
        }
      });
    },
    // 切换角色
    changeRole(idx) {
      this.activeIndex = idx;
      // 1. 获取当前角色下的权限点数据列表
      const roleId = this.roleList[idx].roleId;
      this.getRoleDetail(roleId);
      // 2. 获取当前角色下的成员数据列表
      this.getRoleUserList(roleId);
    },
    //获取角色列表
    async getRoleList() {
      const res = await getRoleListAPI();
      this.roleList = res.data;
      // console.log(this.roleList);
    },
    // 获取功能权限
    async getTreeList() {
      const res = await getTreeListAPI();
      this.treeList = res.data;
      // 禁用
      // 目标：treeList里面的每一项以及嵌套的子项都添加一个disabled = true
      // console.log(this.treeList);
      this.addDisabled(this.treeList);
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-tree-node__label {
  color: rgba(0, 0, 0, 0.3);
}
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding: 20px;
  ::v-deep .el-tree-node__content label.el-checkbox.is-checked {
    span.is-checked .el-checkbox__inner {
      background-color: #0094ff;
    }
  }
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>
